package br.ucb.conexao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import br.ucb.excecao.RegrasException;

public abstract class Conexao_1 {
	
	private static Connection conexao;

	private static Connection createConnection() throws RegrasException {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		String url = "jdbc:mysql://localhost:3306/papiros";
		String user = "root";
		String password = "root";
		
		Connection conexao = null;
		
		try {
			conexao = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			throw new RegrasException(e.getMessage());
		}
		
		return conexao;
	}
	
	public static void closeConexao(){
		try {
			getConexao().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		conexao = null;
	}

	public static Connection getConexao() {
		if(conexao==null)
			conexao=createConnection();
		return conexao;
	}
}
